package main

//https://godoc.org/fyne.io/fyne doc文档
import (
	"fmt"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	"io/ioutil"
	"net/url"
)

func init() {

}
func main() {
	a := app.New()
	//加载资源
	file, _ := ioutil.ReadFile("C:\\work\\文档\\tmp\\头像.png")
	icon := fyne.NewStaticResource("sdfsd", file)

	//设置标题图标
	a.SetIcon(icon)

	//创建工具栏
	toolbar := widget.NewToolbar(widget.NewToolbarAction(icon, func() {
		fmt.Println("sdfasdf")
	}))

	//创建窗口
	w := a.NewWindow("Hello")
	//设置窗口size
	w.Resize(fyne.NewSize(300, 200))

	//创建菜单
	mainMenu := fyne.NewMainMenu(
		fyne.NewMenu("file",
			fyne.NewMenuItem("open", func() {
				fmt.Printf("sdf")
			})),
	)
	w.SetMainMenu(mainMenu)

	box := container.NewHBox(widget.NewLabel("Hello Fyne!"), widget.NewButton("Quit", func() {
		a.Quit()
	}))

	//浏览器打开指定的地址
	url, _ := url.Parse("http://www.baidu.com")
	button := widget.NewButton("sdfsdf", func() {
		fyne.CurrentApp().OpenURL(url)
		fmt.Printf("sdfadsf")
	})

	button2 := widget.NewButton("bu2", func() {
		fmt.Printf("剪贴板内容：%s", "dfds")
	})

	//展示在屏幕中间
	w.CenterOnScreen()

	//监听键盘点击事件
	w.Canvas().SetOnTypedKey(func(event *fyne.KeyEvent) {
		fmt.Printf("%s", event.Name)
	})
	w.Canvas().SetOnTypedRune(func(r rune) {
		fmt.Printf("%s", r)
	})
	//设置窗体的内容
	w.SetContent(container.NewHBox(
		box,
		button,
		button2,
		toolbar,
	))

	//展示窗体
	w.ShowAndRun()
}
